package com.glo.quiki.service;

import com.glo.quiki.model.LoginResult;
import com.glo.quiki.model.QuikiPaymentOrder;
import com.glo.quiki.req.QuikiRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class QuikiService {


    /**
     *
     * @param email 邮箱
     * @param password 密码
     * @return csrfToken
     */
    public LoginResult login(String email, String password){

        String csrfToken = QuikiRequest.loadingLoginPage();
        System.out.println(csrfToken);

        Map<String, String> accountInfo = new HashMap<>();
        //token
        accountInfo.put("_token", csrfToken);
        //账号
        accountInfo.put("email", email);
        //密码
        accountInfo.put("password", password);
        accountInfo.put("remember", "on");

        return QuikiRequest.submitLoginRequest(accountInfo);
    }

    public List<QuikiPaymentOrder>  selectPaymentOrder(String email, String password,String startDate,String endDate,String remberCookie) throws Exception {
        LoginResult loginResult = new LoginResult();
        if (remberCookie == null){
            loginResult = login(email, password);
        }
        return QuikiRequest.sendSelectPaymentOrder(loginResult, LocalDate.parse(startDate, DateTimeFormatter.ofPattern("MM/dd/yyyy")),LocalDate.parse(endDate, DateTimeFormatter.ofPattern("MM/dd/yyyy")));
    }


    public boolean withdrawOrder(String email, String password, String cuvAccount, String orderAmount,String rememberCookie) throws Exception{
        LoginResult loginResult = new LoginResult();
        loginResult.setRememberCookie(rememberCookie);
        if (rememberCookie == null){
            loginResult = login(email, password);
        }
        QuikiRequest.sendCreateTransferOrder(loginResult,cuvAccount,orderAmount);
        return false;
    }
}
